<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="单据号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder= queryParams.orderId
          :disabled="true"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="制单人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          :disabled="true"
          placeholder= queryParams.createBy
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>




      <el-form-item label="供应商" prop="supplierName">
        <el-select v-model="queryParams.supplierName" placeholder="供应商" clearable size="small">
          <el-option
            v-for="item in supplierNameOptions"
            :key="item.supplierName"
            :label="item.supplierName"
            :value="item.supplierName"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="总批发额" prop="orderSumprice">
        <el-input
          v-model="queryParams.orderSumprice"
          :disabled="true"
          placeholder="请输入关键字"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ypjxc:buy:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ypjxc:buy:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ypjxc:buy:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ypjxc:buy:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="buyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="药品ID" align="center" prop="buyId" />
      <el-table-column label="药品名称" align="center" prop="drugName" />
      <el-table-column label="规格" align="center" prop="buyUnit" />
      <el-table-column label="生产厂家" align="center" prop="drugFactory" />
      <el-table-column label="采购数量" align="center" prop="buyNum" />
      <el-table-column label="批发价" align="center" prop="buySuprice" />
      <el-table-column label="批发额" align="center" prop="buyPrice" />
      <el-table-column label="批次号" align="center" prop="buyBatchnum" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ypjxc:buy:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ypjxc:buy:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采购对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>

      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="药品名称" prop="orderName">
          <el-input
            v-model="queryParams.orderName"
            placeholder= queryParams.orderName
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="药品类型" prop="drugType">
          <el-select v-model="queryParams.drugType" placeholder="供应商" clearable size="small">
            <el-option
              v-for="item in drugTypeOptions"
              :key="item.drugType"
              :label="item.drugType"
              :value="item.drugType"
            />
          </el-select>
        </el-form-item>

          <el-form-item label="药品类型" prop="drugType">
            <el-select v-model="queryParams.drugRecipetype" placeholder="供应商" clearable size="small">
              <el-option
                v-for="item in drugRecipetypeOptions"
                :key="item.drugRecipetype"
                :label="item.drugRecipetype"
                :value="item.drugRecipetype"
              />
            </el-select>


            <el-table v-loading="loading" :data="buyList" @selection-change="handleSelectionChange">
              <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="药品ID" align="center" prop="buyId" />
              <el-table-column label="药品名称" align="center" prop="drugName" />
              <el-table-column label="药品编码" align="center" prop="drugCode" />
              <el-table-column label="生产厂家" align="center" prop="drugFactory" />
              <el-table-column label="药品类型" align="center" prop="drugType" />
              <el-table-column label="处方类型" align="center" prop="drugRecipetype" />
            </el-table>

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBuy, getBuy, delBuy, addBuy, updateBuy, exportBuy,listFactory,listBuyType,getOrderId,getSupplier } from "@/api/ypjxc/buy";
import Data from "../../system/dict/data";

export default {
  name: "Buy",
    components: {Data},
    data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购表格数据
      buyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
        supplierNameOptions:[],
        drugTypeOptions:[],
        factoryNameOptions:[],
        drugRecipetypeOptions:[],
        buyRecipetype:[],
        buyType:[],
        buyFactory:[],


      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
          orderId:undefined,
          createBy:undefined,
          supplierName:undefined,
          orderSumprice:undefined,
          drugRecipetype:undefined,
          drugType:undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        buyName: [
          { required: true, message: "采购名称不能为空", trigger: "blur" }
        ],
        buyCode: [
          { required: true, message: "采购编码不能为空", trigger: "blur" }
        ],
        buySort: [
          { required: true, message: "采购顺序不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
      getOrderId().then(response => {
          this.queryParams.orderId = response.orderId;
          this.queryParams.createBy=response.createBy;
      });
      getSupplier().then(response => {
          this.supplierNameOptions = response.data;
      })
  },
  methods: {
    /** 查询采购列表 */
    getList() {
      this.loading = true;
      listBuy(this.queryParams).then(response => {
        this.buyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 采购状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        drugName: undefined,
        buyId: undefined,
        drugCode: undefined,
          buyNum: undefined,
          buySuprice: undefined,
          buyBatchnum:undefined,
          drugFactory:undefined,
          buyPrice:undefined,
          buyNum:undefined,

          drugRecipetype:undefined,

          drugType: undefined,
        status: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.buyId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const buyId = row.buyId || this.ids
      getBuy(buyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改采购";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.buyId != undefined) {
              updateBuy(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBuy(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const buyIds = row.buyId || this.ids;
      this.$confirm('是否确认删除采购编号为"' + buyIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBuy(buyIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有采购数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportBuy(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
